import InputField from '@/components/hhd/design/FormField/fields/InputField.vue'
import MoneySearch from '@comp/hhd/design/FormField/searchFieds/MoneySearch'
import NumberSearch from '@comp/hhd/design/FormField/searchFieds/NumberSearch'
import DateSearch from '@comp/hhd/design/FormField/searchFieds/DateSearch'
import staffField from '@comp/hhd/design/FormField/fields/business/staff/StaffField'
import DepartField from '@comp/hhd/design/FormField/fields/business/depart/DepartField'
import UnitField from '@comp/hhd/design/FormField/fields/business/unit/UnitField'
import SelectSearch from '@comp/hhd/design/FormField/fields/SelectSInput'
import {getFieldConfigById} from '@/utils/FormDesignUtils';

export default {
  input: InputField,
  text: InputField,
  money: MoneySearch,
  number: NumberSearch,
  date: DateSearch,
  dateTime: DateSearch,
  radio: SelectSearch,
  checkbox: SelectSearch,
  select: SelectSearch,
  selectS: SelectSearch,
  staff: staffField,
  staffs: staffField,
  depart: DepartField,
  departs: DepartField,
  unit: UnitField,
  units: UnitField
}

const conditionType = {
  like: 'like',
  notLike: 'notLike',
  equal: 'equal',
  notEqual: 'notEqual',
  great: 'great',
  greatEqual: 'greatEqual',
  less: 'less',
  lessEqual: 'lessEqual',
  between: 'between',
}

const conditionTypeTitle = {
  equal: '等于',
  notEqual: '不等于',
  like: '包含',
  notLike: '不包含',
  great: '大于',
  greatEqual: '大于等于',
  less: '小于',
  lessEqual: '小于等于',
  between: '范围',
}


const componentSearchType = {
  input: {
    default: conditionType.like,
    conditions: [conditionType.like,  conditionType.notLike, conditionType.equal,  conditionType.notEqual]
  },
  number: {
    default: conditionType.between,
    conditions: [conditionType.equal,  conditionType.notEqual, conditionType.great, conditionType.greatEqual, conditionType.less, conditionType.lessEqual]
  },
  money: {
    default: conditionType.between,
    conditions: [conditionType.equal,  conditionType.notEqual, conditionType.great, conditionType.greatEqual, conditionType.less, conditionType.lessEqual]
  },
  date: {
    default: conditionType.between,
    conditions: [conditionType.equal,  conditionType.notEqual, conditionType.great, conditionType.greatEqual, conditionType.less, conditionType.lessEqual]
  },
  dateTime: {
    default: conditionType.between,
    conditions: [conditionType.equal,  conditionType.notEqual, conditionType.great, conditionType.greatEqual, conditionType.less, conditionType.lessEqual]
  },
  radio: {
    default: conditionType.like,
    conditions: [conditionType.like,  conditionType.notLike, conditionType.equal,  conditionType.notEqual]
  },
  checkbox: {
    default: conditionType.like,
    conditions: [conditionType.like,  conditionType.notLike, conditionType.equal,  conditionType.notEqual]
  },
  select: {
    default: conditionType.like,
    conditions: [conditionType.like,  conditionType.notLike, conditionType.equal,  conditionType.notEqual]
  },
  selectS: {
    default: conditionType.equal,
    conditions: [conditionType.like,  conditionType.notLike, conditionType.equal,  conditionType.notEqual]
  },
  staff: {
    default: conditionType.like,
    conditions: [conditionType.like,  conditionType.notLike, conditionType.equal,  conditionType.notEqual]
  },
  staffs: {
    default: conditionType.like,
    conditions: [conditionType.like,  conditionType.notLike, conditionType.equal,  conditionType.notEqual]
  },
  depart: {
    default: conditionType.like,
    conditions: [conditionType.like,  conditionType.notLike, conditionType.equal,  conditionType.notEqual]
  },
  departs: {
    default: conditionType.like,
    conditions: [conditionType.like,  conditionType.notLike, conditionType.equal,  conditionType.notEqual]
  },
  unit: {
    default: conditionType.like,
    conditions: [conditionType.like,  conditionType.notLike, conditionType.equal,  conditionType.notEqual]
  },
  units: {
    default: conditionType.like,
    conditions: [conditionType.like,  conditionType.notLike, conditionType.equal,  conditionType.notEqual]
  },
}

export {componentSearchType, conditionType, conditionTypeTitle}



export function buildQuery(searchModel, formDefinition, searchFields) {
  let res = { conditions: [] }, fields = formDefinition.fields || []
  searchFields.forEach(f => {
    let field = fields.find(field => field.id === f.fieldId)
    if (!field) return
    let condition = createCondition(searchModel, field, f)
    if (condition) {
      if (Array.isArray(condition)) {
        res.conditions.push(...condition)
      } else {
        res.conditions.push(condition)
      }
    }
  })

  return res
}

export function createCondition(searchModel, fieldConfig, searchField) {
  let conditionType = componentSearchType[fieldConfig.component]
  if (!conditionType) {
    console.error('不支持搜索: ' + fieldConfig.component)
    return
  }

  return conditionBuilder[`${conditionType}Condition`](searchModel, fieldConfig.fieldName)
}

const conditionBuilder = {
  betweenCondition(searchModel, fieldName) {
    let list = searchModel[fieldName], res = []
    if (list && list[0]) {
      res.push(newCondition(fieldName, conditionType.greatEqual, list[0]))
    }
    if (list && list[1]) {
      res.push(newCondition(fieldName, conditionType.lessEqual, list[1]))
    }
    if (res.length) return res;
  },
  likeCondition(searchModel, fieldName) {
    if (searchModel[fieldName]) {
      return newCondition(fieldName, conditionType.like, `%${searchModel[fieldName]}%`)
    }
  },
  eqCondition(searchModel, fieldName) {
    if (searchModel[fieldName]) {
      return newCondition(fieldName, conditionType.like, `%${searchModel[fieldName]}%`)
    }
  },
  geCondition(searchModel, fieldName) {
    if (searchModel[fieldName]) {
      return newCondition(fieldName, conditionType.ge, searchModel[fieldName])
    }
  },
  leCondition(searchModel, fieldName) {
    if (searchModel[fieldName]) {
      return newCondition(fieldName, conditionType.le, searchModel[fieldName])
    }
  }
}

export function newCondition(fieldName, condition, value) {
  return { fieldName, condition, value }
}